const app = getApp()
const constant = require("../../constant.js")
const storage = require("../../utils/storage.js")
const util = require("../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      host: constant.api(app.globalData.pord).url,
      qiniu: constant.resource.qiniu,
      mobile: wx.getStorageSync(storage.keys.mobile)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;

    wx.request({
      url: that.data.host + '/coach/schedules?mobile=' + that.data.mobile,
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          that.setData({
            result: res.data.data,
          });
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000
          });
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '请求数据失败',
          icon: 'none',
          duration: 1000
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  changeWeek: function(e) {
    this.request(e.currentTarget.dataset.week);
  },

  request: function (day) {
    var that = this;

    wx.request({
      url: that.data.host + '/coach/schedules?mobile=' + that.data.mobile + '&day=' + day,
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          that.setData({
            result: res.data.data,
          });
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000
          });
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '请求数据失败',
          icon: 'none',
          duration: 1000
        })
      }
    })
  },

  onRest: function () {
    var that = this;

    wx.showModal({
      title: '确认',
      content: '是否确认清空所有休息时间?',
      success: function (res) {
        if (res.confirm) {
          var day = util.formatDate(that.data.result.currentDay);
          wx.request({
            url: that.data.host + '/coach/schedules/reset?mobile=' + that.data.mobile + '&day=' + day,
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            success: function (res) {
              if (res.data.code == "0") {
                wx.showToast({
                  title: res.data.message,
                  icon: 'success',
                  duration: 1000,
                  mask: true,
                  success: function () {
                    that.request(day);
                  }
                });
              } else {
                wx.showToast({
                  title: res.data.message,
                  icon: 'none',
                  duration: 1000
                })
              }
            },
            fail: function (res) {
              wx.showToast({
                title: '请求失败',
                icon: 'none',
                duration: 1000
              })
            }
          })
        }
      }
    })
  },

  onCopy: function () {
    var that = this;

    wx.showModal({
      title: '确认',
      content: '是否确认复制上一周休息时间?',
      success: function (res) {
        if (res.confirm) {
          var day = util.formatDate(that.data.result.currentDay);
          wx.request({
            url: that.data.host + '/coach/schedules/copy?mobile=' + that.data.mobile + '&day=' + day,
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            success: function (res) {
              if (res.data.code == "0") {
                wx.showToast({
                  title: res.data.message,
                  icon: 'success',
                  duration: 1000,
                  mask: true,
                  success: function () {
                    that.request(day);
                  }
                });
              } else {
                wx.showToast({
                  title: res.data.message,
                  icon: 'none',
                  duration: 1000
                })
              }
            },
            fail: function (res) {
              wx.showToast({
                title: '请求失败',
                icon: 'none',
                duration: 1000
              })
            }
          })
        }
      }
    })
  }
})